<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <search ref='search' :config='searchConfig' @search='search' @getHeight='getHeight' />

    <!-- table区域-begin -->
    <div class='page_table'>
      <vxe-grid
        ref='xGrid'
        :border='true'
        :resizable='true'
        :showOverflow='true'
        size='mini'
        :loading='loading'
        :height='pageHeight-seachHeight-90'
        :align='left'
        :columns='columns'
        :data='dataSource'
        :toolbar-config='tableToolbar'
        :checkbox-config="checkboxConfig"
        v-bind='vexConfig'
        @checkbox-change='selectChangeEvent'
        @checkbox-all='selectChangeEvent'
      >
        <template #toolbar_buttons>
          <div class='table-operator'>
            <a-button type="primary" @click="downExportXls('visitInfo_report')">导出</a-button>
          </div>
        </template>

        <template #status='{ row }'>
          <a-switch :checked='row.status' size='small' @change='handleStatus(row,$event)' />
        </template>
        <template #action='{ row:record }'>
          <a @click='handleEdit1(record)'>编辑</a>
          <span v-if='record.fileUrl'>
            <a-divider type='vertical' />
            <a :href='record.fileUrl'>下载</a>
          </span>
        </template>
        <template #pager>
          <vxe-pager
            :layouts="['Total', 'PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump','Sizes', 'FullJump']"
            :currentPage.sync='ipagination.current'
            :page-size.sync='ipagination.pageSize'
            :total='ipagination.total'
            :pageSizes='ipagination.pageSizeOptions'
            @page-change='handlePageChange'>
          </vxe-pager>
        </template>
      </vxe-grid>
    </div>
    <!-- table区域-end -->

  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { PageListMixin } from '@/mixins/PageListMixin'
import moment from 'moment'
import { postAction } from '../../../api/manage'
export default {
  name: 'SfaVisitLineList',
  mixins: [PageListMixin],
  components: {

  },
  data() {
    return {
      description: '进销存报表页面',
      operType: 0,
      //查询区域
      searchConfig:[
        {
          type: 'input',
          name: 'visitPosCode',
          placeholder: '选择人员'
        },
        {
          type: 'date-range', //范围选择
          range: ['startDate', 'endDate'],
          name: 'range',
          format: 'YYYY-MM-DD',
          placeholder: ['创建时间', '创建时间']
        },
        {
          type: 'date',
          name: 'createDate',
          placeholder: '请选择日期'
        },
      ],
      // 表头
      columns: [
        { type: 'checkbox', width: 50, fixed: 'left' },
        {
          title: '优先级',
          width:80,
          field: 'priority'
        },
        {
          title: '网点类型',
          width:80,
          field: 'clientType',
          customRender: function(text) {
            if (text === '1') {
              return '门店'
            }else {
              return '经销商'
            }
          }
        },
        {
          title: '网点编码',
          width:80,
          field: 'clientCode'
        },
        {
          title: '网点名称',
          width:80,
          field: 'clientName'
        },
        {
          title: '拜访人',
          width:80,
          field: 'visitUserName'
        },
        {
          title: '拜访人名',
          width:80,
          field: 'visitUserRealname'
        },
        {
          title: '拜访人职位',
          width:100,
          field: 'visitPosCode'
        },
        {
          title: '拜访人职位名',
          width:120,
          field: 'visitPosName'
        },
        {
          title: '拜访开始日期',
          width:120,
          field: 'visitDateStart'
        },
        {
          title: '拜访结束日期',
          width:120,
          field: 'visitDateEnd'
        },
        {
          title: '拜访类型',
          width:80,
          field: 'visitType',
          customRender: function(text) {
            if (text === '0') {
              return '临时'
            }else  if (text === '1') {
              return '计划'
            }else  if (text === '2') {
              return '协访'
            }
          }
        },

        {
          title: '拜访状态',
          width:80,
          field: 'visitStatus',
          customRender: function(text) {
            if (text === 0) {
              return '未拜访'
            }else  if (text === 1) {
              return '拜访中'
            }else  if (text === 2) {
              return '已拜访'
            }
          }
        },
        {
          title: '创建人姓名',
          width:100,
          field: 'createName'
        },
        {
          title: '创建时间',
          width:80,
          field: 'createDate'
        },

        {
          title: '更新时间',
          width:80,
          field: 'updateDate'
        },

        {
          title: '更新人姓名',
          width:100,
          field: 'updateName'
        },
        {
          title: '停用启用状态',
          width:120,
          field: 'status',
          customRender: function(text) {
            if (text === '009') {
              return '启用'
            } else {
              return '停用'
            }
          }
        },
        {
          title: '是否删除',
          width:80,
          field: 'deleteStatus',
          customRender: function(text) {
            if (text === '009') {
              return '否'
            } else {
              return '是'
            }
          }
        },
        {
          title: '操作',
          field: 'action',
          fixed: 'right',
          width:80,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: window._CONFIG['domianSfaURL'] + '/visit/sfaVisitLineController/findPageList',
        delete: window._CONFIG['domianSfaURL'] + '/visit/sfaVisitLineController/delete',
        deleteBatch: window._CONFIG['domianSfaURL'] + '/visit/sfaVisitLineController/deleteBatch',
        exportXlsUrl: window._CONFIG['domianSfaURL'] + 'visit/sfaVisitLineController/exportXls',
        importExcelUrl: window._CONFIG['domianSfaURL'] + 'visit/sfaVisitLineController/importExcel'
      }
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },

  methods: {
    //限制当天之前的日期不可选
    disabledDate(current) {
      // return current && current <moment().subtract(1, "days"); //当天之前的不可选，不包括当天
      return current && current < moment().endOf('day') //当天之前的不可选，包括当天
    },
    searchQuery() {
      if (this.checkParam(false)) {
        this.loadData(1)
      }
    },
    handleAddLineByGroup() {
      if (this.checkParam(true)) {
        this.operType = 2
        this.$refs.JVisitGroupModal.showModal(this.queryParam.visitPosCode)
      }

    },
    handleAddLine() {
      if (this.checkParam(true)) {
        this.operType = 1
        this.$refs.JVisitClientModal.showModal(this.queryParam.visitPosCode)
      }

    },
    checkParam(ischeck) {
      if (ischeck) {
        if (!this.queryParam.visitPosCode) {
          this.$message.warning('请选择拜访人')
          return false
        }
        if (!this.queryParam.visitDates || this.queryParam.visitDates.length == 0) {
          this.$message.warning('请选择拜访时间')
          return false
        }
        let temp = []
        this.queryParam.visitDates.map(row => {
          temp.push(moment(row).format('YYYY-MM-DD'))
        })
        this.queryParam.visitDates = temp
      } else {
        this.queryParam.tempVisitDate = moment(this.queryParam.tempVisitDate).format('YYYY-MM-DD')
      }
      return true
    },
    handelSave(clients, groupCode) {
      const record = {
        visitPosCode: this.queryParam.visitPosCode,
        visitDates: this.queryParam.visitDates,
        operType: this.operType
      }
      if (this.operType === 1) {
        record.lineVos = clients
      } else {
        record.groups = clients
      }
      var that = this
      postAction(window._CONFIG['domianSfaURL'] + '/visit/sfaVisitLineController/saveOrUpdate', record).then(async (res) => {
        if (res.success) {
          that.$message.success(res.message)
          await this.loadData()
          if (this.operType === 1) {
            this.$refs.JVisitClientModal.close()
          } else {
            this.$refs.JVisitGroupModal.close()
          }
        } else {
          this.$message.warning(res.message)
        }
      }).finally(() => {
        if (this.operType === 1) {
          this.$refs.JVisitClientModal.confirmLoading = false
        } else {
          this.$refs.JVisitGroupModal.confirmLoading = false
        }
      })
    },
    async handleClientModal(record) {
      if (this.selectedRowKeys.length != 1) {
        this.$message.warning('请选择一条记录！')
        return
      }
      this.$refs.JSelectClientModal.showModal(this.selectionRows[0].groupCode)
    },
    handleAddUser() {
      this.$refs.VisitConfigModal.showModal()
    },
    handelSaveOk(pos){
      if (pos!=null && pos !=null){
        this.$set(this.queryParam,"visitPosCode",pos.positionCode);
        this.$set(this.queryParam,"queryVisitPosName",pos.positionName+"-"+pos.realname);
      }
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
